package org.mybatis.generator.api.plus;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.*;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author wangfupeng
 */
public class MybatisPluginComment extends PluginAdapter {


    private FullyQualifiedJavaType schema;
//    private FullyQualifiedJavaType apiModel;
//    private FullyQualifiedJavaType apiModelProperty;
    private boolean addGWTInterface;
    private boolean suppressJavaInterface;
    private FullyQualifiedJavaType data;
    private FullyQualifiedJavaType noArgsConstructor;
    private FullyQualifiedJavaType builder;
    private FullyQualifiedJavaType allArgsConstructor;

    public MybatisPluginComment() {
        super();
//        apiModel = new FullyQualifiedJavaType("io.swagger.annotations.ApiModel");
//        apiModelProperty = new FullyQualifiedJavaType("io.swagger.annotations.ApiModelProperty");
        schema = new FullyQualifiedJavaType("io.swagger.v3.oas.annotations.media.Schema");
        allArgsConstructor = new FullyQualifiedJavaType("lombok.AllArgsConstructor");
        builder = new FullyQualifiedJavaType("lombok.Builder");
        data = new FullyQualifiedJavaType("lombok.Data");
        noArgsConstructor = new FullyQualifiedJavaType("lombok.NoArgsConstructor");
    }

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
                                                 IntrospectedTable introspectedTable) {
        topLevelClass.getJavaDocLines().clear();
//        topLevelClass.addImportedType(apiModelProperty);
        topLevelClass.addImportedType(schema);
        topLevelClass.addImportedType(data);
        topLevelClass.addImportedType(builder);
        topLevelClass.addImportedType(allArgsConstructor);
        topLevelClass.addImportedType(noArgsConstructor);
        topLevelClass.addAnnotation("@Schema(description=\"" + introspectedTable.getRemarks() + "\")");
        topLevelClass.addAnnotation("@Data");
        topLevelClass.addAnnotation("@Builder");
        topLevelClass.addAnnotation("@NoArgsConstructor");
        topLevelClass.addAnnotation("@AllArgsConstructor");
        return true;
    }

    @Override
    public boolean modelFieldGenerated(Field field,
                                       TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                       IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        field.getJavaDocLines().clear();
        field.addJavaDocLine("/**");
        field.addJavaDocLine(" * Table:     " + introspectedTable.getFullyQualifiedTable());
        field.addJavaDocLine(" * Column:    " + introspectedColumn.getActualColumnName());
        field.addJavaDocLine(" * Nullable:  " + introspectedColumn.isNullable());
        field.addJavaDocLine(" */");
        field.addAnnotation("@Schema(description=\"" + introspectedColumn.getRemarks().replaceAll("\"", "“") + "\")");
        return true;
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method,
                                              TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method,
                                              TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        method.getJavaDocLines().clear();
        return true;
    }


}
